package com.cn.yunhe.rentcars.web;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.captcah.BusiLogUtil;
import com.cn.yunhe.rentcars.mapper.AccountMapper;
import com.cn.yunhe.rentcars.mapper.BusiLogMapper;
import com.cn.yunhe.rentcars.mapper.RoleMapper;
import com.cn.yunhe.rentcars.model.Account;
import com.cn.yunhe.rentcars.model.Role;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 权限管理--系统用户管理
 * @author 赵志学
 *
 */
@RestController
public class AccountController {
	
	@Autowired
	private AccountMapper accountMapper;//将用户信息操作接口注入
	/**
	 * 注入日志操作
	 */
	@Autowired
	private BusiLogMapper busiLogMapper;
	@Autowired
	private RoleMapper roleMapper;
	
	//TODO 添加用户信息 URL=/accounts/add，method=POST
	/**
	 * 跳转到添加用户页面
	 * @return
	 */
	@RequestMapping(value="/accountadd",method=RequestMethod.GET)
	public ModelAndView addAccount(){
		Map<String,String> map = new HashMap<String,String>();		
		List<Role> roleList = roleMapper.findRoles(map);
		Map<String,Object> rMap = new HashMap<String,Object>();
		rMap.put("roleList",roleList);
		return new ModelAndView("account-add",rMap);
	}
	/**
	 * 从页面获取添加系统用户是的信息。持久化
	 * @param account 系统用户对象
	 */
	@RequestMapping(value="/saveAddAccount",method=RequestMethod.POST)
	public void saveAddAccount(@RequestBody Account account){
		int count = accountMapper.selectCount();//获取当前数据库中的记录总数。		
		account.setUid(Integer.toString(count+1));//将当前数据库的记录数+1 作为新添加的 用户ID。
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		account.setAddtime(sdf.format(date));	
		accountMapper.addAccount(account);//将页面提交的用户信息，持久化。
		
	}
	//TODO 修改用户信息 URL=/accountsedit，method=PUT 
	/**
	 * 根据页面选择的系统用户ID 号，从数据库读出 用户的详细信息，并且传递到 修改用户信息页面 
	 * @return 返回到页面
	 */
	@RequestMapping(value="/accountsedit",method=RequestMethod.GET)
	public ModelAndView selectRow(@RequestParam String uid){
		//System.out.println("修改用户信息，获取用户id:"+uid);
		Account ac = accountMapper.selectRow(uid);
		//System.out.println("对应的用户名："+ac.getUsername());
		Map<String,Account> map = new HashMap<String,Account>();
		map.put("account", ac);
		return new ModelAndView("account-edit",map) ;		
	}	
	/**
	 * 保存修改从页面上提交的修改后的系统用户信息，
	 */
	@RequestMapping(value="/saveEditAccount/{uid}",method=RequestMethod.PUT)
	public void editAccount(@PathVariable String uid , @RequestBody Account account){
		account.setUid(uid);
		accountMapper.editAccount(account);		
	}
	//TODO 删除用户信息  URL=/accountsdelete，method=DELETE
	@RequestMapping(value="/accountsdelete",method=RequestMethod.POST)
	public void delAccount(@RequestParam String uid){
		accountMapper.deleteAccount(uid);		
	}
	//TODO 批量删除用户信息  URL=
	@RequestMapping(value="/detAccounts",method=RequestMethod.POST)
	public void delAccounts(@RequestParam String usersid){		
		List<String> uid = Arrays.asList(usersid.split(","));		
		accountMapper.deleteAccounts(uid);
	}
	//TODO 用户管理 首页面（列表页面）/getAccountList URL=/accountslist，method=GET
	@RequestMapping(value="/getAccountList",method=RequestMethod.GET)
	public Map<String,Object> getAccountList(@RequestParam int start,@RequestParam int length,@RequestParam int draw,@RequestParam String username,@RequestParam String mobile,@RequestParam String personid ){
		//System.out.println("进入获取信息页面");
		Map<String,String> map = new HashMap<String,String>();
		map.put("username", username);		
		map.put("mobile", mobile);
		map.put("personid",personid);
		int pageNum = start/length+1;
		int pageSize = length;	
		PageHelper.startPage(pageNum,pageSize);
		//System.out.println("分页信息初始化完毕，准备进入数据库操作");
		//List<Account> accountList = accountMapper.getAccounts(username);
		List<Account> accountList = (accountMapper.findAccounts(map));
				
		PageInfo<Account> page = new PageInfo<Account>(accountList);
		 Map<String,Object> rsMap = new HashMap<String,Object>();
	      rsMap.put("draw",draw);
	      rsMap.put("data", page.getList());	      
	      rsMap.put("recordsTotal", page.getTotal());
	     rsMap.put("recordsFiltered", page.getTotal());
	   return rsMap;
	}
	@RequestMapping(value="/accountslist",method=RequestMethod.GET)
	public ModelAndView selectAccountList(){
		return new ModelAndView("account-list");
	}
	@RequestMapping(value="/setAccountState",method=RequestMethod.POST)
	public void handleAccountState(@RequestParam String uid,@RequestParam Integer userstate){
		//System.out.println("修改用户信息ID :"+uid+" sta:"+userstate);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("uid",uid);			
		map.put("userstate",userstate);
		//System.out.println("修改用户信息ID :"+uid+" sta:"+userstate);
		accountMapper.handleAccountState(map);
		//System.out.println("修改成功");
	}
	/**
	 * @author 朱钦明添加方法
	 * 
	 * 跳转到修改密码页面
	 * @return ModelAndView跳转到account-editpwd页面
	 */
	@RequestMapping(value="/account-editpwd",method=RequestMethod.GET)
	public ModelAndView editPwd(HttpServletRequest request){
		String username = (String) request.getSession().getAttribute("username");
		
		Account account = accountMapper.getAccountByName(username);		
	
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("account",account);
		map.put("username", username);
		return new ModelAndView("account-editpwd",map);
	}
	/**
	 * 首页个人信息查看，根据当前用户的名称，查询用户信息。
	 * @param username
	 * @return
	 */
	@RequestMapping(value="/accountChectMessage" , method=RequestMethod.GET)
	public ModelAndView accountCheckMessage(@RequestParam String username){			
		Account ac = accountMapper.getAccountRole(username);		
		Map<String,Account> rsMap = new HashMap<String,Account>();
		rsMap.put("account",ac);
		return new ModelAndView("account-check",rsMap);
} 
	
	/**
	 * @author 朱钦明添加方法
	 * 
	 * 修改个人信息
	 * @param uid 用户编号
	 * @param username 用户名
	 * @param personid 员工工号
	 * @param realname 员工姓名
	 * @param newpass 新密码
	 * @param userpwd 原密码
	 * @param oldpass 需要验证的原密码
	 */
	@RequestMapping(value="/accountedit/{uid}",method=RequestMethod.POST)
	public Map<String,String> updateAccount(@PathVariable("uid") String uid,@RequestParam String username,
			@RequestParam String personid,@RequestParam String realname,@RequestParam String newpass,@RequestParam String userpwd,@RequestParam String oldpass,HttpServletRequest request){
		//验证个人输入的原密码与数据库的原密码是否相同，不相同则返回一个map
		if(oldpass != null && !userpwd.equals(oldpass) && !oldpass.equals("")){
			Map<String,String> map = new HashMap<String,String>();
			map.put("false", "false");
			return map;
		}
		//修改个人信息
		Map<String,Object> accMap = new HashMap<String,Object>();
		accMap.put("uid", uid);
		accMap.put("username", username);
		accMap.put("personid", personid);
		accMap.put("realname", realname);
		accMap.put("newpass", newpass);
		accountMapper.updateAccountPass(accMap);
		//添加日志信息
        Map<String,Object> bus1Map = new HashMap<String,Object>();
        bus1Map.put("logid", UUID.randomUUID().toString());
        bus1Map.put("businame", "根据字典id修改字典信息");
        bus1Map.put("busitype", "修改");
        bus1Map.put("busiconent","根据字典id修改字典信息");
        BusiLogUtil.addBusiLog(request, busiLogMapper, bus1Map);
		return null;
	}
}
